package com.yykj.hospital.server.service.impl;

import com.yykj.common.enums.ResultEnums;
import com.yykj.common.exception.NormalException;
import com.yykj.common.util.LocalObjectUtil;
import com.yykj.common.util.PageRequestUtil;
import com.yykj.common.vo.PageData;
import com.yykj.hospital.server.form.HospitalTrafficEditForm;
import com.yykj.hospital.server.form.HospitalTrafficListForm;
import com.yykj.hospital.server.model.entity.HospitalGuide;
import com.yykj.hospital.server.model.entity.HospitalTraffic;
import com.yykj.hospital.server.model.repository.HospitalTrafficRepository;
import com.yykj.hospital.server.service.HospitalTrafficService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;

@Service
public class HospitalTrafficServiceImpl implements HospitalTrafficService {
    @Override
    public HospitalTraffic editHospitalTraffic(HospitalTrafficEditForm form) {

        HospitalTraffic hospitalTraffic;
        if (form.getId() != null && form.getId() > 0) {
            hospitalTraffic = trafficRepository.findById(form.getId()).orElseThrow(() -> new NormalException(ResultEnums.NOT_EXIST));
        } else {
            hospitalTraffic = new HospitalTraffic();
        }
        LocalObjectUtil.copyNullValue(form, hospitalTraffic);
        hospitalTraffic = trafficRepository.save(hospitalTraffic);

        return hospitalTraffic;
    }

    @Override
    public void deleteHospitalTraffic(Integer id) {
        trafficRepository.deleteById(id);
    }

    @Override
    public PageData<HospitalTraffic> listHospitalTraffic(HospitalTrafficListForm form) {
        if (form.getHospitalId() == null || form.getHospitalId() <= 0) {
            throw new NormalException(ResultEnums.PARAM_ERROR);
        }
        Pageable pageable = PageRequest.of(PageRequestUtil.getPage(form.getCurrentPage()), PageRequestUtil.getSize(form.getPageSize()),Sort.by(Sort.Order.asc("sorting"), Sort.Order.desc("addTime")));
        Page<HospitalTraffic> list = trafficRepository.findAll((Specification<HospitalTraffic>) (root, query, builder) -> {
            List<Predicate> predicateList = new ArrayList<>();

            predicateList.add(builder.equal(root.get("hospitalId"), form.getHospitalId()));

            if (!StringUtils.isEmpty(form.getKeyword())) {
                predicateList.add(builder.like(root.get("title"), "%" + form.getKeyword() + "%"));
            }
            if (form.getStatus() != null) {
                predicateList.add(builder.equal(root.get("status"), form.getStatus()));
            }

            Predicate[] predicates = new Predicate[predicateList.size()];
            return query.where(predicateList.toArray(predicates)).getRestriction();
        }, pageable);
        return PageData.formatList(list,form.getCurrentPage(),form.getPageSize());
    }

    private HospitalTrafficRepository trafficRepository;

    @Autowired
    public void setTrafficRepository(HospitalTrafficRepository trafficRepository) {
        this.trafficRepository = trafficRepository;
    }
}
